<template>
  <div class="app-main" v-loading="loading">
    <div class="divTmp" v-show="loading"></div>
    <div class="course-resource-page course-second-page student-model"
         v-show="!loading">
      <div class="resource-page-header course-second-header clearfix">
        <ol class="breadcrumb">
          <span>当前位置：</span>
          <li>
            <a class="breadcrumb-step">全部资源</a>
          </li>
        </ol>
        <div class="dropdown pull-left clearfix">
          <el-select style="height: 32px!important;"
                     v-model="type"
                     placeholder="全部类型">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                @click.native="HandleCommand">
            </el-option>
          </el-select>

        </div>
        <div class="input-search-group">
          <input type="text"
                 class="text-input text-input-gray"
                 v-model="input"
                 placeholder="输入关键字搜索">
          <i @click="" class="el-icon-search glyphicon glyphicon-search"></i>
        </div>
      </div>

      <empty v-show="!showList"
             info="暂无资源"/>

      <ul v-show="showList" class="resource-list">
        <li v-for="item in resourceList">
          <i class="iconfont icon-fujianleixingppt icon-ppt"
             style="color: #f36666"
             v-if="item.type === 'ppt'"></i>
          <i class="iconfont icon-fujianleixingppt icon-word"
             style="color: #409EFF"
             v-else-if="item.type === 'word'"></i>
          <i class="iconfont icon-fujianleixingppt icon-weibiaoti-_huabanfuben"
             style="color: rgba(201,213,114,0.59)"
             v-else-if="item.type === 'file'"></i>
          <i class="iconfont icon-fujianleixingppt icon-shipin"
             style="color: rgba(201,213,114,0.59)"
             v-else-if="item.type === 'video'"></i>
          <div class="detail">
            <div class="title ellipsis-two" @click="openIframe">
              {{ item.title }}
            </div>
            <div class="info">
              <span style="margin-right: 20px;">{{ item.name }}</span>
              <span>{{ item.createTime }}</span>
            </div>
          </div>
          <div class="status">
            <span v-show="!item.status">未查看</span>
            <span v-show="item.status">已查看</span>
          </div>
          <div class="operate">
            <button class="button button-resource-view"
                    :class="{'button-red-solid': item.status,
                  'button-red-hollow': !item.status}"
                    @click="openIframe(item.courseId,item.teacherId)">查看
            </button>
          </div>
          <!--          iframe-->
          <div class="resource-preview-container" v-show="open">
            <div class="resource-preview-header">
              <h4 class="ellipsis-one">{{ item.title }}</h4>
              <button class="button button-red-hollow">下载</button>
              <button class="button button-red-hollow" @click="cacel">关闭</button>
            </div>
            <div class="resource-preview-body">
              <div class="resource-content">
                <div class="res-document">
                  <iframe
                      :src="item.url"/>
                </div>
              </div>
            </div>
          </div>

        </li>
      </ul>

      <div v-show="showList" class="pagination-container">
        <el-pagination
            class="pagination-wrap pagination-resource-list"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="pagination.page"
            :page-size="pagination.pageSize"
            layout="prev,pager,next, jumper"
            :total="pagination.counts">
        </el-pagination>
      </div>
    </div>
  </div>


</template>

<script>
import {resourceGet} from "@/apis/student";
import {resourceUpdate} from "@/apis/resource";
import Empty from './Empty'

export default {
  components: {
    Empty,
  },
  props: {
    course: {},
  },
  data() {
    return {
      loading: true,
      input: '',
      open: false,
      pagination: {
        status: 1,
        pageSize: 8,
        page: 1,
        name: '',
        counts: 0,
        id: '',

      },
      type: '',
      showList: false,
      resourceList: {},
      options: [{
        value: '',
        label: '全部选项'
      }, {
        value: 'microLessons',
        label: '微课',
      }, {
        value: 'video',
        label: '视频'
      }, {
        value: 'mp3',
        label: '音频'
      }, {
        value: 'ppt',
        label: '图片'
      }, {
        value: 'file',
        label: '文档'
      }, {
        value: 'link',
        label: '链接'
      }, {
        value: 'photo',
        label: '图文'
      }, {
        value: 'other',
        label: '其他'
      },
      ],
    }
  },
  created() {

    this.init()
  },

  methods: {
    // 初始化资源
    async init() {
      this.closeLoading()
      let id = localStorage.getItem("id")
      // 获取课程对应资源
      let data = {
        id: id,
        course: this.course,
        page: this.pagination.page,
        pageSize: this.pagination.pageSize,
        name: this.input,
        type: this.type,
      }
      resourceGet(data).then((resp) => {
        if (resp?.data?.data?.records.length > 0) {
          this.resourceList = resp?.data?.data?.records
          this.pagination.counts = resp?.data?.data?.total
          this.showList = true
        }

      })

    },
    // 切换菜单栏
    HandleCommand() {
      this.resourceList = {}
      this.loading = true
      this.showList = false
      this.init()
    },

    // 打开iframe
    openIframe(courseId, teacherId) {
      console.log(courseId)
      let id = localStorage.getItem("id")
      resourceUpdate(id, courseId, teacherId).then((resp) => {

      })
      this.open = true
    },
    // 分页菜单页码的改变
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.init(this.isActive)
    },
    handleCurrentChange(val) {
      this.pagination.page = val
      this.init(this.isActive)
    },
    // 取消
    cacel() {

      this.open = false
      this.init()
    },


    // 加载动画
    closeLoading() {
      this.timer = null
      this.timer = setTimeout(() => {
        this.loading = false
      }, 500)
    }
  }
}
</script>

<style scoped>

div {
  display: block;
  unicode-bidi: isolate;
}

*, :after, :before {
  box-sizing: border-box;
}

.divTmp {
  width: 100%;
}

::v-deep .el-loading-mask {
  transition: opacity 0s;
}

::v-deep .el-loading-spinner {
  top: 100%;
  margin-top: 30%;
  width: 100%;
  text-align: center;
  position: absolute;
}

.pull-left {
  float: left !important;
}
::v-deep .el-input__icon {
  height: 115%;
}
::v-deep .el-input--suffix .el-input__inner {
  height: 32px;
  background-color: #efeff7;
  color: #444444;
}
.input-search-group {
  margin-right: 12px;
  width: 200px;
  border-radius: 999px;
  background-color: #efeff7;
  position: relative;
  display: inline-block;
  height: 32px;
  line-height: 30px;
  font-size: 14px;
  vertical-align: middle;
}

.text-input {
  padding-left: 16px;
  padding-right: 34px;
  background-color: transparent;
  border: none;
  min-width: 200px;
  max-width: 400px;
  height: 100%;
  line-height: 30px;
  outline: none;
  overflow: hidden;
  text-overflow: ellipsis;
  box-sizing: border-box;
}

.glyphicon {
  right: 10px;
  margin: auto;
  cursor: pointer;
  position: absolute;
  top: 0;
  bottom: 0;
  height: 16px;
  font-size: 16px;
  color: #cbcbd1;
}

.resource-page-header {
  padding-bottom: 27px;
  border-bottom: 1px solid #e3e3e9;
}

.breadcrumb {
  margin-top: 4px;
  margin-bottom: 19px;
  padding: 0;
  background-color: transparent;
  border-radius: 4px;
  position: relative;
  z-index: 2;
  list-style: none;
}

.breadcrumb > span {
  vertical-align: middle;
}

.breadcrumb > li {
  margin-right: -2px;
  display: inline-block;
}

.breadcrumb-step {
  color: #7691fc;
}

.breadcrumb-step {
  display: inline-block;
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown {
  height: 32px;
  min-width: 110px;
  max-width: 110px;
  position: relative;
  margin-bottom: 10px;
  margin-right: 10px;
}

.btn-default {
  height: 32px;
  padding: 5px 16px 5px 10px;
  text-align: left;
  background-color: #efeff7;
  border-color: #e3e3e9;
  width: 100%;
  color: #969696;
  outline: none;
  display: block;
  border-radius: 4px;
}

::v-deep .el-icon--right {
  margin-left: 15px;
}


ul {
  padding: 0;
  margin: 0;
  list-style: none;
}

.resource-list li {
  padding: 12px 10px;
  border-bottom: 1px dashed #e3e3e9;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
}

.resource-list li > i {
  margin-right: 8px;
  align-self: self-start;
  font-size: 20px;
  line-height: 1;
}

.icon-fujianleixingppt {
  color: #f36666;
}

.detail {
  padding-right: 22px;
  -ms-flex: 1;
  flex: 1;
}

.title {
  cursor: pointer;
}

.ellipsis-two {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info {
  margin-top: 4px;
  font-size: 12px;
  line-height: 18px;
  color: #969696;
}

.status {
  padding: 0 10px;
  width: 150px;
  color: #969696;
}

.operate {
  padding: 0 10px;
}

.button-red-solid {
  background-color: #7691fc;
  border-color: #7691fc;
}

.button-red-solid:hover {
  background-color: #90a6fc;
  border-color: #90a6fc;
}

.button-red-hollow {
  background-color: #fff;
  border-color: #e3e3e9;
  color: #7691fc;
}

.button-red-hollow:hover {
  border-color: #7691fc;
}

.resource-preview-container {
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, .8);
  position: fixed;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  z-index: 99999;
  top: 0;
  bottom: 0;
}

.resource-preview-header {
  padding: 0 20px;
  background-color: rgba(0, 0, 0, .5);
  display: -ms-flexbox;
  display: flex;
  height: 50px;
  -ms-flex-align: center;
  align-items: center;
}

.resource-preview-header h4 {
  margin-right: 20px;
  -ms-flex: 1;
  flex: 1;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  margin-top: 10px;
  margin-bottom: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.1;
}

.button {
  margin-left: 10px;
}

.hnylbk .button-red-hollow:hover {
  border-color: #7691fc;
  color: #7691fc;
}


.resource-preview-body {
  -ms-flex: 1;
  flex: 1;
  height: 0;
  overflow: hidden;
}

.resource-content {
  padding: 20px 20px 40px;
  height: 100%;
}

.res-document {
  height: 100%;
  text-align: center;
}

.res-document iframe {
  background: #fff;
  width: 940px;
  height: 100%;
  vertical-align: middle;
}

.pagination-container {
  text-align: center;
}

.pagination-wrap {
  padding: 20px 0;
}
</style>